package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.Device;
import com.ruoyi.mes.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.DeviceRepairInfoMapper;
import com.ruoyi.mes.domain.DeviceRepairInfo;
import com.ruoyi.mes.service.IDeviceRepairInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备维修Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-09-27
 */
@Service
public class DeviceRepairInfoServiceImpl implements IDeviceRepairInfoService
{
    @Autowired
    private DeviceRepairInfoMapper deviceRepairInfoMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询设备维修
     *
     * @param id 设备维修主键
     * @return 设备维修
     */
    @Override
    public DeviceRepairInfo selectDeviceRepairInfoById(Long id)
    {
        return deviceRepairInfoMapper.selectDeviceRepairInfoById(id);
    }

    /**
     * 查询设备维修列表
     *
     * @param deviceRepairInfo 设备维修
     * @return 设备维修
     */
    @Override
    public List<DeviceRepairInfo> selectDeviceRepairInfoList(DeviceRepairInfo deviceRepairInfo)
    {
        return deviceRepairInfoMapper.selectDeviceRepairInfoList(deviceRepairInfo);
    }

    /**
     * 新增设备维修
     *
     * @param deviceRepairInfo 设备维修
     * @return 结果
     */
    @Override
    public AjaxResult insertDeviceRepairInfo(DeviceRepairInfo deviceRepairInfo)
    {
        //新增时修改设备的状态
        Device device = deviceMapper.selectDeviceById(deviceRepairInfo.getDeviceId());

        if (deviceRepairInfo.getRepairResult() == 0){//无法修复
            device.setAvailableStatus(3L);
        } else if (deviceRepairInfo.getRepairResult() == 1) {// 部分修复
            device.setAvailableStatus(2L);
        } else if (deviceRepairInfo.getRepairResult() == 2) {//修复成功
            device.setAvailableStatus(0L);
        }
        deviceMapper.updateDevice(device);

        deviceRepairInfo.setCreateTime(DateUtils.getNowDate());
        deviceRepairInfoMapper.insertDeviceRepairInfo(deviceRepairInfo);
        return AjaxResult.success("添加成功!");
    }

    /**
     * 修改设备维修
     *
     * @param deviceRepairInfo 设备维修
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDeviceRepairInfo(DeviceRepairInfo deviceRepairInfo)
    {
        Device device = deviceMapper.selectDeviceById(deviceRepairInfo.getDeviceId());

        if (deviceRepairInfo.getRepairResult() == 0){//无法修复
            device.setAvailableStatus(3L);
        } else if (deviceRepairInfo.getRepairResult() == 1) {// 部分修复
            device.setAvailableStatus(2L);
        } else if (deviceRepairInfo.getRepairResult() == 2) {//修复成功
            device.setAvailableStatus(0L);
        }
        deviceMapper.updateDevice(device);

        deviceRepairInfo.setUpdateTime(DateUtils.getNowDate());
        return deviceRepairInfoMapper.updateDeviceRepairInfo(deviceRepairInfo);
    }

    /**
     * 批量删除设备维修
     *
     * @param ids 需要删除的设备维修主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRepairInfoByIds(Long[] ids)
    {
        return deviceRepairInfoMapper.deleteDeviceRepairInfoByIds(ids);
    }

    /**
     * 删除设备维修信息
     *
     * @param id 设备维修主键
     * @return 结果
     */
    @Override
    public int deleteDeviceRepairInfoById(Long id)
    {
        return deviceRepairInfoMapper.deleteDeviceRepairInfoById(id);
    }
}
